import { EVector2, EVector3 } from "../engine/Math";
import { MoveBody } from "./MoveBody";
/**
 * 地图管理器
 * 支持一张完整的地图根据等级不同开启不同范围
 */
export default class MoveMap {

    //完整地图的宽高纵深
    protected size: EVector3 = new EVector3(0, 0, 0)

    //地图开放的范围宽高纵深
    protected rangeSize: EVector3 = new EVector3(0, 0, 0)
    //摄像机可到达的最左侧坐标，最底部坐标，最外侧坐标
    protected rangePos: EVector3 = new EVector3(0, 0, 0)

    protected anchor: EVector2 = new EVector2(0.5, 0.5)

    protected topRight: EVector2 = new EVector2(0, 0)


    setAnchor(x: number, y: number) {
        this.anchor.x = x;
        this.anchor.y = y;
    }

    getAnchorX() {
        return this.anchor.x;
    }

    getAnchorY() {
        return this.anchor.y;
    }


    init(w: number, h: number, r: number = 0) {
        console.log(' GameMap w ', w, ' h ', h, ' r ', r)
        if (this.size.x != w || this.size.y != h || this.size.z != r) {
            this.size.x = w;
            this.size.y = h;
            this.setMapDepth(r)
            this.setRangeWidth(this.size.x)
            this.setRangeHeight(this.size.y)
            this.setRangeDepth(this.size.z)
        }
    }

    setMapWidth(w: number) {
        this.size.x = w;
    }

    setMapDepth(d: number) {
        this.size.z = d;
        this.updateTop();
    }


    /**
     * 是否在地图内
     * @param x 
     * @param y 
     */
    isInMap(x: number, y: number, width: number, height: number, ax: number = 0.5, ay: number = 0.5) {
        let left: number = x - width * ax;
        let right: number = x + width * ax;
        let up: number = y + height * ay;
        let down: number = y - height * ay;
        return right >= this.getRangeX() && left <= this.getRangeWidth() && up >= this.getRangeY() && down <= this.getRangeHeight();
        // return x >= 0 && x <= this.getRangeWidth() && y >= 0 && y <= this.getRangeHeight();
    }

    isOutMap(x: number, y: number, width: number, height: number, ax: number = 0.5, ay: number = 0.5) {
        let left: number = x - width * ax;
        let right: number = x + width * ax;
        let up: number = y + height * ay;
        let down: number = y - height * ay;
        return right < 0 || left > this.getRangeWidth() || up < this.getRangeY() || down > this.getRangeHeight()
    }

    isInRange(move: MoveBody) {
        return this.isInMap(move.getX(), move.getY(), move.getWidth(), move.getHeight(), move.getAnchorX(), move.getAnchorY())
    }


    isOutRange(move: MoveBody) {
        if (!move) {
            return false;
        }
        return this.isOutMap(move.getX(), move.getY(), move.getWidth(), move.getHeight(), move.getAnchorX(), move.getAnchorY())
    }


    getMapWidth() {
        return this.size.x
    }

    getMapHeight() {
        return this.size.y
    }

    getMapDepth() {
        return this.size.z;
    }

    getTop() {
        return this.topRight.x;
    }

    getRight() {
        return this.topRight.y;
    }

    setRangeX(x: number) {
        this.rangePos.x = x;
        this.updateRight()
    }

    getRangeX() {
        return this.rangePos.x;
    }

    setRangeY(y: number) {
        this.rangePos.y = y;
    }

    getRangeY() {
        return this.rangePos.y
    }

    setRangeZ(z: number) {
        this.rangePos.z = z;
        this.updateTop();
    }

    updateTop() {
        this.topRight.x = this.getRangeZ() + this.getMapDepth();
    }

    updateRight() {
        this.topRight.y = this.getRangeX() + this.getRangeWidth()
    }

    getRangeZ() {
        return this.rangePos.z;
    }

    setRangeWidth(w: number) {
        this.rangeSize.x = w;
        this.updateRight()
    }

    getRangeRight(width: number) {
        return this.getRangeX() + this.getRangeWidth() - width
    }

    setRangeDepth(r: number) {
        this.rangeSize.z = r;
    }

    getRangeDepth() {
        return this.rangeSize.z;
    }

    getRangeTop(height: number) {
        return this.getRangeY() + this.getRangeHeight() - height
    }


    getRangeWidth() {
        return this.rangeSize.x;
    }

    setRangeHeight(h: number) {
        this.rangeSize.y = h;
    }

    getRangeHeight() {
        return this.rangeSize.y
    }



}